home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5621 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: news.zocalo.net!news
  2. From: Paul Hsieh <qed@xenon.chromatic.com>
  3. Newsgroups: comp.lang.c,comp.graphics.algorithms,rec.games.programmer
  4. Subject: Re: Speed question here...
  5. Date: Mon, 19 Feb 1996 21:15:43 -0800
  6. Organization: Zocalo Engineering - Berkeley, California, USA
  7. Message-ID: <312958FF.6F1C@xenon.chromatic.com>
  8. References: <4ftluh$1gkv@hearst.cac.psu.edu> <4g9rbg$3cl@ooze.val.net>
  9. NNTP-Posting-Host: paulh.chromatic.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win95; I)
  14.  
  15. Andrea Griffini wrote:
  16. > koscho@wjk130.rh.psu.edu (William Koscho) wrote:
  17. > >I was curious as to how fast something like the
  18. > >following would execute:
  19. > >       int x;
  20. > >       node *ptr;   ptr in linked list
  21. > >       for ( ptr = first_node; ptr != NULL; ptr = ptr.next ) {
  22. > >          for ( x = 0; x < max; x++ ) {
  23. > >               array[x] = array_other[x];
  24. > >           }
  25. > >      }
  26.  
  27.  
  28. Uh ... unless array and ptr are somehow linked in a way that is not
  29. obvious here, this code is awefully silly.  Chances are you meant
  30. something more along the lines of:
  31.  
  32.     for( ptr=first_node; ptr!=NULL; ptr=ptr->next ) {
  33.         for( x=0; x<max; x++ ) {
  34.             ptr->array[x] = array_init[x];
  35.         }
  36.     }
  37.  
  38. N'est-ce pas?   In this case, you are overlooking obvious potential 
  39. for platform specific improvements.  Most CPUs have some sort of
  40. memory copy that's faster than element by element assignment.  With-
  41. out resorting to assembly language, this is available in the form
  42. of "memcpy".  So more than likely this is a better solution:
  43.  
  44.     int arraylen = max*sizeof(ptr->array[0]);
  45.  
  46.     for( ptr=first_node; ptr!=NULL; ptr=ptr->next ) {
  47.         memcpy( ptr->array, array_init, arraylen);
  48.     }
  49.  
  50. Moral of the story:  Know your C library.
  51.  
  52. -- 
  53. Paul Hsieh
  54. qed@xenon.chromatic.com
  55.  
  56. What I say and what my company says are not always the same thing
  57.